// .........................................................................
// Title: bartik_instrument.do
//
// Constructs Bartik shift-share instrument based on purchases at the time
// of bond issuance
// .........................................................................

* --------------------------------------
* Assignments indicators
* --------------------------------------

* issuance dates
use "$tmp/insurer_summary_trace_q_ig", clear
gen issuance_quarter = qofd(issuance_date)
gcollapse (firstnm) issuance_quarter, by(cusip)
drop if missing(cusip) | missing(issuance_quarter)
save "$tmp/_issuance_quarters", replace

use "$raw/cmns/gcap_security_master_cusip", clear
keep cusip issuance_date
gen issuance_quarter = qofd(issuance_date)
gcollapse (firstnm) issuance_quarter, by(cusip)
drop if missing(cusip) | missing(issuance_quarter)
mmerge cusip using "$tmp/_issuance_quarters", update
drop _merge
save "$tmp/issuance_quarters", replace

* buying indicators
use "$insurance_holdings/all_insurance_master", clear
cap rename entity_name holder_name
keep holder_name cusip date_q marketvalue_usd
mmerge cusip using "$tmp/issuance_quarters", unmatched(m)
format %tq issuance_quarter
keep if ~missing(issuance_quarter)
drop _merge
keep if date_q == issuance_quarter
keep holder_name cusip issuance_quarter marketvalue_usd
gen buyer_at_issuance = 1
gsort holder_name issuance_quarter cusip
by holder_name issuance_quarter cusip: keep if _n == 1
rename issuance_quarter date_q
rename marketvalue_usd position_at_issuance
save "$tmp/instrument_buying_indicators", replace

* --------------------------------------
* Construct instrument
* --------------------------------------

* insurer average purchase size
use "$tmp/holdings_at_issuance_by_insurer_ig", clear
gcollapse (mean) marketvalue_usd, by(holder_name date_q)
bys date_q: egen totVal = total(marketvalue_usd)
gen purchase_size_xs = marketvalue_usd / totVal
gcollapse (mean) purchase_size_xs, by(holder_name)
egen totShare = total(purchase_size_xs)
replace purchase_size_xs = purchase_size_xs / totShare
drop totShare
save "$tmp/insurer_avg_purchase_sizes", replace

* bonds with issuance data
use "$tmp/instrument_buying_indicators", clear
keep cusip
bys cusip: keep if _n == 1
save "$tmp/instrument_buying_indicators_cat", replace

* constructing pre-05 buying indicators for bonds issued prior to 2005, based on 2005 holdings
use "$insurance_holdings/all_insurance_master", clear
cap rename entity_name holder_name
keep holder_name cusip date_q marketvalue_usd
mmerge cusip using "$tmp/instrument_buying_indicators_cat", unmatched(m)
drop if missing(cusip)
drop if _merge == 3
gen year = year(dofq(date_q))
keep if year == 2005
mmerge cusip using "$raw/cmns/gcap_security_master_cusip", ukeep(issuance_date) unmatched(m)
mmerge cusip using "$tmp/ciq_static_characteristics_processed", unmatched(m) ukeep(iq_offer_date) uname(_c_)
replace issuance_date = _c_iq_offer_date if missing(issuance_date)
gen issuance_quarter = qofd(issuance_date)
keep if ~missing(issuance_quarter) & issuance_quarter < tq(2005q1)
gcollapse (max) marketvalue_usd, by(holder_name cusip)
save "$tmp/pre05_buying_indicators", replace

* append post-05 and buying indicators based on 2005 data for bonds issued prior to that year
use "$tmp/instrument_buying_indicators", clear
gen post05 = 1
mmerge holder_name cusip using "$tmp/pre05_buying_indicators"
assert post05 == 1 if _merge == 1
assert post05 == . if _merge == 2
replace post05 = 0 if _merge == 2
replace position_at_issuance = marketvalue_usd if missing(position_at_issuance) & post05 == 0
drop _merge marketvalue_usd
assert buyer_at_issuance == 1 if post05 == 1
replace buyer_at_issuance = 1
assert ~missing(date_q) if post05 == 1
assert missing(date_q) if post05 == 0
replace date_q = tq(2005q1) if post05 == 0

* merge in average insurer purchase size
mmerge holder_name using "$tmp/insurer_avg_purchase_sizes", unmatched(m)

* collapse to form the instrument
gen instrument = purchase_size_xs * buyer_at_issuance
gcollapse (sum) instrument (min) post05, by(cusip)

* standardize the instrument to unit variance
egen sd_instrument = sd(instrument)
egen mean_instrument = mean(instrument)
gen instrument_norm = instrument / sd_instrument
save "$tmp/security_bartik_instrument", replace
